﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.MVVM.UI;
using Habi.Solver.ViewModels;
using DevExpress.XtraEditors;
using Habi.Solver.ViewModels.Resources;
using Habi.Solver.Models.Resources;
using Habi.Solver.Services;
using System.Linq.Expressions;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using DotNetExtensions.AspNet.DevExpress.Services;

namespace Habi.Solver.Views.Resources
{
    [ViewType(MainViewModel.UploadResourceView)]
    public partial class UploadResourceView : XtraUserControl
    {
        private readonly IValidationService validationService;
        public UploadResourceView()
        {
            InitializeComponent();

            validationService = new ValidationService(dxValidationProvider1);
            mvvmContext.ViewModelType = typeof(UploadResourceViewModel);

            mvvmContext.RegisterService(new ControlOperationService(this));
            mvvmContext.RegisterService(new DialogMessageBoxService(this));
            mvvmContext.RegisterService(validationService);

            if (!mvvmContext.IsDesignMode) InitializeView();
        }

        void InitializeView()
        {
            var fluent = mvvmContext.OfType<UploadResourceViewModel>();

            fluent.SetBinding(bindingSource1, b => b.DataSource, x => x.Tasks);

            bindingSource1.DataSourceChanged += (sender, e) =>
            {
                Console.Write(e);
            };

            fluent.SetCommandTrigger(x => x.Open(), () =>
              {

              }, DevExpress.Utils.MVVM.CommandTriggerExecutionMode.AfterCommand);

            fluent.WithEvent(this, "Load").EventToCommand(x => x.Load());

            fluent.SetItemsBinding(comboBoxEditCategory.Properties, x => x.Categorys);
            fluent.SetBinding(listBoxControl, t => t.DataSource, x => x.Files);

            fluent.SetBinding(comboBoxEditCategory, t => t.EditValue, x => x.Category);
            fluent.SetBinding(textEditPublisher, t => t.Text, x => x.Description);
            fluent.SetBinding(textEditAboutUrl, t => t.Text, x => x.AboutUrl);

            fluent.BindCommand(simpleButtonOpen, x => x.Open());
            fluent.BindCommand(simpleButtonRemove, x => x.Remove(null), x => x.Selected);

            listBoxControl.SelectedValueChanged += (sender, e) =>
            {
                var files = listBoxControl.SelectedItems.Select(c => (FileModel)c).ToList();
                fluent.ViewModel.Selected = files;
            };

            fluent.BindCommandAndImage(windowsUIButtonPanel.Buttons[2], x => x.Save());
            fluent.BindCommandAndImage(windowsUIButtonPanel.Buttons[0], x => x.Close());

            validationService.SetRules(comboBoxEditCategory, new ConditionValidationRule { ErrorText = "Not Blank", ConditionOperator = ConditionOperator.IsNotBlank });
            validationService.SetRules(textEditAboutUrl, new ConditionValidationRule { ErrorText = "Not Blank", ConditionOperator = ConditionOperator.IsNotBlank });
            validationService.SetRules(textEditPublisher, new ConditionValidationRule { ErrorText = "Not Blank", ConditionOperator = ConditionOperator.IsNotBlank });
            validationService.SetRules(richTextBoxDescription, new ConditionValidationRule { ErrorText = "Not Blank", ConditionOperator = ConditionOperator.IsNotBlank });
            validationService.SetRules(comboBoxEditCategory, new ConditionValidationRule { ErrorText = "Not Blank", ConditionOperator = ConditionOperator.IsNotBlank });
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }
}
